package com.GXKJ.Test.controller;


import com.GXKJ.Test.config.DynamicDataSource;
import com.GXKJ.Test.entity.Databasesource;
import com.GXKJ.Test.entity.User;
import com.GXKJ.Test.service.DatabasesourceService;
import com.GXKJ.Test.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author NongZhiQuan
 * @since 2021-05-14 10:18:55
 */

@RestController
@RequestMapping("/databasesource")
public class DatabasesourceController {
    @Autowired
    DatabasesourceService databasesourceService;
    @Autowired
    UserService userService;

    @Autowired
    DynamicDataSource dynamicDataSource;

    private final static Logger log = LoggerFactory.getLogger(DatabasesourceController.class);


//    @GetMapping("/testUser")
//    public String test1(String datasourceid) throws Exception {
//        databasesourceService.changeDb(datasourceid);
//        List<User> userList1=userService.selectAllUser();
//        System.out.println(userList1.toString());
//        DBContextHolder.clearDataSource();//切换回主数据库
//        return "OK!";
//    }

    @GetMapping("/test1")
    public String test1(String datasourceid,String url,String dbname,String username,String password) throws Exception {
        Databasesource databasesource=new Databasesource();
        databasesource.setDatasourceid(datasourceid);
        databasesource.setUrl(url);
        databasesource.setDbname(dbname);
        databasesource.setUsername(username);
        databasesource.setPassword(password);

        List<Databasesource> list=databasesourceService.get();
        for (Databasesource DBListResult:list) {
            System.out.println("DBListResult:"+DBListResult);
        }

        log.info("检测到输入的数据源ID为："+databasesource.getDatasourceid());
        if (list.contains(databasesource)==true){
            dynamicDataSource.createDataSourceWithCheck
                    (databasesource.getDatasourceid(),databasesource.getUrl(),
                            databasesource.getDbname(),databasesource.getUsername(),databasesource.getPassword());

            if (DynamicDataSource.DBContextHolder.setDataSource(databasesource.getDatasourceid())==true){
                List<User> listUser=userService.selectAllUser();
                listUser.forEach(db -> {
                    System.out.println(db.getId()+db.getName());
                });
                log.info("查询完成!");
                DynamicDataSource.DBContextHolder.clearDataSource();
                return "ok!";
            }else {
                log.info("切换至输入的数据源:"+databasesource.getDatasourceid()+"--失败,请重新检查一下!");
                DynamicDataSource.DBContextHolder.clearDataSource();
                return "No OK!";
            }
        }else {
            log.info("输入的数据源与系统里的数据源不匹配!应该是输入格式不正确!");
            DynamicDataSource.DBContextHolder.clearDataSource();
        }
        return "代码执行完成!";
    }


//    @GetMapping("/test2")
//    public Object test2(Integer datasourceid,String url,String dbname,String username,String password) throws Exception {
//        List<Databasesource> list=databasesourceService.get();
//        for (Databasesource DBListResult:list) {
//            System.out.println("DBListResult:"+DBListResult);
//        }
//        Databasesource dbDatabasesource=list.get(datasourceid);
//        databasesourceService.changeDb(datasourceid,url,dbname,username,password);
//        //创建数据源
////        try{
////            dynamicDataSource.createDataSourceWithCheck(dbDatabasesource);
////        }catch (Exception e) {
////            e.printStackTrace();
////        }
//        //切换数据源
//        //DynamicDataSource.DBContextHolder.setDataSource(dbDatabasesource.getDatasourceid());
//        List<User> listUser=userService.selectAllUser();
//        listUser.forEach(db -> {
//            System.out.println(db.getId()+db.getName());
//        });
//        DynamicDataSource.DBContextHolder.clearDataSource();//切换回主数据库
//        return "ok!";
//    }
}

